﻿// Test_SIPClient.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "pch.h"
#include <Windows.h>
#include <tchar.h>
using namespace std;
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/XyRyNet_CommHdr.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_Client/NetClient_Socket/NetClient_Error.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_Sip/RfcSip_Define.h"
#include "../../../../NetEngine_Windows/NetEngine_SourceCode/NetEngine_RfcComponents/RfcComponents_Sip/RfcSip_Error.h"

#pragma comment(lib,"Ws2_32.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/NetClient_Socket.lib")
#pragma comment(lib,"../../../../NetEngine_Windows/NetEngine_SourceCode/Debug/RfcComponents_Sip.lib")

SOCKET hUDPSocket = 0;
XNETHANDLE xhToken = 0;
DWORD WINAPI NetClient_UDPSelect_Thread(LPVOID lParam)
{
	TCHAR tszClientAddr[64];
	TCHAR tszMsgBuffer[2048];

	while (1)
	{
		int nMsgLen = 2048;
		SIPPROTOCOL_HDRINFO st_SIPProtocol;
		memset(&st_SIPProtocol, '\0', sizeof(SIPPROTOCOL_HDRINFO));

		memset(tszClientAddr, '\0', sizeof(tszClientAddr));
		memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
		if (NetClient_UDPSelect_RecvMsg(hUDPSocket, tszMsgBuffer, &nMsgLen, tszClientAddr))
		{
			RfcComponents_SipProtocol_Parse(tszMsgBuffer, nMsgLen, &st_SIPProtocol);
			RfcComponents_SipClient_Process(xhToken, &st_SIPProtocol);

			//如果是呼叫,我们需要返回一个响铃给对方
			if (0 == _tcsnicmp(st_SIPProtocol.st_Request.tszMethod, XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_INVITE, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_INVITE)))
			{
				memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
				NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
				printf("Recv:%d\n,%s\n", nMsgLen, tszMsgBuffer);
				//处理后的值是180,表示成功,才可以返回
				if (st_SIPProtocol.st_Response.nCode == 180)
				{
					Sleep(2000);
					st_SIPProtocol.st_Response.nCode = 200;       //等待两秒我们接起
					RfcComponents_SipClient_RepInvite(xhToken, &st_SIPProtocol);
					memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
					RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
					NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
				}
			}
			else if (0 == _tcsnicmp(st_SIPProtocol.st_Request.tszMethod, XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_MESSAGE, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_MESSAGE)))
			{
				printf("NEW MESSAGE %s:%s\n", st_SIPProtocol.st_From.tszName, st_SIPProtocol.st_Context.tszBodyBuffer);
				//清理SDP
				memset(&st_SIPProtocol.st_Context, '\0', sizeof(st_SIPProtocol.st_Context));

				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
				NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
			}
			else if (0 == _tcsnicmp(st_SIPProtocol.st_Request.tszMethod, XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_CANCEL, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_CANCEL)))
			{
				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
				NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);

				if (200 == st_SIPProtocol.st_Response.nCode)
				{
					//继续返回一个481错误给客户端
					st_SIPProtocol.st_Response.nCode = 481;
					RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
					NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
				}
			}
			else if (0 == _tcsnicmp(st_SIPProtocol.st_Request.tszMethod, XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_OPTIONS, _tcslen(XENGINE_RFCCOMPONENTS_SIP_PROTOCOL_STR_TYPE_OPTIONS)))
			{
				printf("NEW OPTION %s\n", st_SIPProtocol.st_From.tszName);
				RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
				NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
			}
			//如果是响应并且为成功
			if (!st_SIPProtocol.bRequest && (200 == st_SIPProtocol.st_Response.nCode))
			{
				//我们看看是应该处理什么
				ENUM_RFCCOMPONENTS_SIP_EVENTS enSIPEvent;
				RfcComponents_SipClient_GetStatus(xhToken, &enSIPEvent);
				if (ENUM_RFCCOMPONENTS_SIP_EVENTS_CALL_COMMUNICATE == enSIPEvent)
				{
					//对话接受呼叫了,需要发送一个ACK给对面
					memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
					RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
					NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
					//通话5秒后挂断
					Sleep(5000);
					memset(tszMsgBuffer, '\0', sizeof(tszMsgBuffer));
					RfcComponents_SipProtocol_PacketResponse(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
					RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
					NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
				}
				else if (ENUM_RFCCOMPONENTS_SIP_EVENTS_MESSAGE_ANSWERED == enSIPEvent)
				{
					printf("ok ENUM_RFCCOMPONENTS_SIP_EVENTS_MESSAGE_ANSWERED\n");
				}
				else if (ENUM_RFCCOMPONENTS_SIP_EVENTS_OPTION_ANSWERED == enSIPEvent)
				{
					printf("ok ENUM_RFCCOMPONENTS_SIP_EVENTS_OPTION_ANSWERED\n");
				}
			}
			Sleep(10);
		}
	}
	return 0;
}
int main(int argc, char *argv[])
{
	WSADATA st_WSAData;
	WSAStartup(MAKEWORD(2, 2), &st_WSAData);
	printf("%d\n", argc);
	int nPort = 0;
	int nMsgLen = 0;
	TCHAR tszUser[64];
	TCHAR tszToAddr[64];
	TCHAR tszToUser[64];
	TCHAR tszMsgBuffer[2048];
	LPCTSTR lpszCodeFile = _T("E:\\netengineapp\\NetEngine_WINApps\\Debug\\SipCode.types");

	memset(tszUser, '\0', 64);
	memset(tszToAddr, '\0', 64);
	memset(tszToUser, '\0', 64);
	memset(tszMsgBuffer, '\0', 2048);
	if (argc > 1)
	{
		nPort = 15052;
		_tcscpy(tszUser, "bob");
	}
	else
	{
		nPort = 15051;
		_tcscpy(tszUser, "alice");
		_tcscpy(tszToAddr, "bob@xyry.org");
		_tcscpy(tszToUser, "bob");
	}
	
	SIPPROTOCOL_HDRINFO st_SIPProtocol;
	memset(&st_SIPProtocol, '\0', sizeof(SIPPROTOCOL_HDRINFO));

	if (!NetClient_UDPSelect_Create(&hUDPSocket, "127.0.0.1", 5000))
	{
		printf("错误\n");
		return -1;
	}
	if (!NetClient_UDPSelect_Bind(hUDPSocket, nPort))
	{
		printf("错误\n");
		return -1;
	}

	CreateThread(NULL, 0, NetClient_UDPSelect_Thread, NULL, 0, NULL);
	RfcComponents_SipConfig_InitCode(lpszCodeFile);
	//注册
	RfcComponents_SipClient_Create(&xhToken, "xyry.org", tszUser, "123123");
	RfcComponents_SipClient_Register(xhToken, &st_SIPProtocol);
	RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer,&nMsgLen);
	NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);

	if (argc <= 1)
	{
		Sleep(1000);
		memset(&st_SIPProtocol, '\0', sizeof(SIPPROTOCOL_HDRINFO));
		memset(tszMsgBuffer, '\0', 2048);
		//填充发送者
		_tcscpy(st_SIPProtocol.st_To.tszName, tszToUser);
		_tcscpy(st_SIPProtocol.st_To.tszAddr, tszToAddr);
		/*呼叫
		//填充SDP
		st_SIPProtocol.st_Context.nBodyLen = 3;
		_tcscpy(st_SIPProtocol.st_Context.tszBodyBuffer, "v=0");
		_tcscpy(st_SIPProtocol.st_Context.tszContentType, "application/sdp");
		//发送
		RfcComponents_SipClient_ReqInvite(xhToken, &st_SIPProtocol);
		RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
		NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);*/

		/*MESSSAGE
		st_SIPProtocol.st_Context.nBodyLen = 11;
		_tcscpy(st_SIPProtocol.st_Context.tszBodyBuffer, "hello world");
		_tcscpy(st_SIPProtocol.st_Context.tszContentType, "text/plain");
		//发送
		RfcComponents_SipClient_ReqMessage(xhToken, &st_SIPProtocol);
		RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
		NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
		*/
		//OPTIONS
		RfcComponents_SipClient_ReqOptions(xhToken, &st_SIPProtocol);
		RfcComponents_SipProtocol_PacketRequest(&st_SIPProtocol, tszMsgBuffer, &nMsgLen);
		NetClient_UDPSelect_SendMsg(hUDPSocket, tszMsgBuffer, nMsgLen);
	}
	while (1)
	{
		Sleep(1000);
	}
	return 0;
}